
import concurrent.futures
import pytest
import os
from time import sleep

from common.myConf import MyConf
from common.my_path import basedir

"""
每个表单下的用例是一个任务。有多少个表单就有多少个任务。
1、pytest.ini 中添加标记
2、测试类添加标记
这个是集成测试，就用main就行了
"""


# 所有的标签名字读取出来、意味着有多少个任务
marks_str = MyConf(os.path.join(basedir, "pytest.ini")).get("pytest","markers")
marks = marks_str.split("\n")[1:]
print(marks)

# 定义一个方法。此方法用来收集每个接口的测试用例，并执行它。
# 需要事先准备：接口名称
def run_cases(mark_name):
    pytest.main(["--alluredir=outputs/reports","-m","{}".format(mark_name)])


with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    future_to_cases = {}
    # 提交子任务
    for mark in marks:
        task = executor.submit(run_cases, mark)
        future_to_cases[task] = mark
        sleep(5)

    print(future_to_cases)
    # 等待子任务完成
    # executor.shutdown(wait=True)
    for future in concurrent.futures.as_completed(future_to_cases):
        try:
            res = future.result()
            print(res)
        except Exception as exc:
            print('generated an exception: %s' % (exc))
